#ifndef HFT_EXECUTION_H
#define HFT_EXECUTION_H

#include "execution/execution_types.h"
#include "execution/order_manager.h"
#include "execution/simulation_engine.h"
#include <yaml-cpp/yaml.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <mutex>
#include <functional>
#include <vector>
#include <spdlog/spdlog.h>

namespace hft {

class Order;
class ExecutionEngine;
using ExecutionCallback = std::function<void(const ExecutionResult&)>;

class ExecutionManager {
public:
    ExecutionManager();
    
    bool init(const YAML::Node& config);
    bool start();
    void stop();
    
    bool submitOrder(const Order& order, ExecutionCallback callback = nullptr);
    bool cancelOrder(const std::string& order_id);
    
    ExecutionStatus getOrderStatus(const std::string& order_id) const;
    std::vector<ExecutionResult> getOrderHistory(const std::string& order_id) const;
    Position getPosition(const std::string& symbol) const;
    Account getAccount() const;

private:
    YAML::Node config_;
    std::shared_ptr<ExecutionEngine> engine_;
    Account account_;
    std::unordered_map<std::string, Position> positions_;
    std::unordered_map<std::string, std::vector<ExecutionResult>> order_history_;
    std::unordered_map<std::string, ExecutionCallback> order_callbacks_;
    mutable std::mutex mutex_;
    bool running_ = false;
    
    void processExecutionResult(const ExecutionResult& result);
    void updatePosition(const ExecutionResult& result);
    void updateAccount(const ExecutionResult& result);
};

} // namespace hft

#endif // HFT_EXECUTION_H